# -*- encoding: utf-8 -*-
'''
@File    :   do_file_score_sort.py
@Time    :   2022/11/11 18:13:22
@Author  :   Cms 
@Version :   1.0
@Contact :   2267000140@qq.com
@WebSite    :   www.baidu.com
'''

# here put the import lib

def input_grade(n):
    with open(r"res.txt",'w') as file:
        for i in range(n):
            name = input("请输入姓名：")
            if i>0:
                file.write('\n'+name)
            else:
                file.write(name)
            file.write(" ")
            test1 = input("请输入语文成绩：")
            file.write(test1)
            file.write(" ")
            test2 = input("请输入数学成绩：")
            file.write(test2)
            file.write(" ")
            test3 = input("请输入外语成绩：")
            file.write(test3)
            # file.write("\n")

def grade_sort():
    list1 = []
    f = open(r"res.txt","r")
    
    lines = f.readlines()      #读取全部内容 ，并以列表方式返回
    for line in lines:
        line = line.strip()#去掉每一行的空格 
        # print(type(line))  
        sublist = line.split(" ")
        # print(type(sublist))
        sublist[1] = int(sublist[1])
        sublist[2] = int(sublist[2])
        sublist[3] = int(sublist[3])
        sublist.append(sublist[1]+sublist[2]+sublist[3])
        # print(sublist)
        list1.append(sublist)
    list2 = sorted(list1, key=lambda st:st[4],reverse=True)
    # print(list2)
    print("姓名","语文","英语","数学","总分")
    for i in list2:
        # print(i)
        for j in i:
            print(j,end='    ') 
        print("")
if __name__ == '__main__':
    n=2
    input_grade(n)
    grade_sort()